import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {BaseService} from '../../../../shared/service/base.service';
import {validMoney} from '../../../../shared/validator/validator';
import {ApiUrl} from '../../../../config/api-url';

@Component({
  selector: 'app-module-edit',
  templateUrl: './module-edit.component.html',
  styleUrls: ['./module-edit.component.less']
})
export class ModuleEditComponent implements OnInit {

  // 模态框表单
  formModel: FormGroup;
  // 加载动画
  isOkLoading: boolean;
  @Input()
  isVisible = false;
  // 父子互传值
  @Output()
  emit: EventEmitter<any> = new EventEmitter();
  @Output()
  editNotice: EventEmitter<any> = new EventEmitter();
  @Input()
  module: any;
  title = "添加模块"
  constructor(private fb: FormBuilder, private baseService: BaseService) { }

  ngOnInit(): void {
    this.buildForm();
  }
  buildForm() {
    this.formModel = this.fb.group({
      id: [0,[]],
      parentId: [null,[]],
      name: [null, [Validators.required]],
      type: [1, [Validators.required]],
      imgUrl: [null, []],
      iconColor: [null, []],
      navUrl: [null, []],
      sort: [null, []],
      allocationType: [false, []],
      status: [false, []],
    });
  }
  ngOnChanges() {
    if (this.module != null) {
      if(this.module.editType == 'insert'){
        this.title = '为"' + this.module.name + '"添加子模块';
        this.formModel.reset({
          id: 0,
          parentId: this.module.parentId,
          name: null,
          type: this.module.type,
          imgUrl: null,
          iconColor: null,
          navUrl: null,
          sort: null,
          allocationType: null,
          status: null,
        });
      }else{
        this.title = '修改模块'
        this.formModel.reset({
          id: this.module.id,
          parentId: this.module.parentId,
          name: this.module.name,
          type: this.module.type,
          imgUrl: this.module.imgUrl,
          iconColor: this.module.iconColor,
          navUrl: this.module.navUrl,
          sort: this.module.sort,
          allocationType: this.module.allocationType,
          status: this.module.status,
        });
      }
    }else{
      this.title = '添加模块';
    }
  }
  handleCancel(){
    this.formModel.reset({
      id: 0,
      parentId: null,
      name: null,
      type: 1,
      imgUrl: null,
      iconColor: null,
      navUrl: null,
      sort: null,
      allocationType: null,
      status: null,
    });
    this.module = null;
    console.log(this.formModel.value);
    this.isVisible = false;
    this.emit.emit();
  }
  handleOk(){
    this.isOkLoading = true;
    if (!this.baseService.zorro.validateFormValidate(this.formModel)) {
      this.isOkLoading = false;
      return;
    }

    if (!this.isOkLoading) {
      return;
    }
    this.formModel.controls['allocationType'].setValue(this.formModel.controls['allocationType'].value ? 1 : 2);
    this.baseService.post(ApiUrl.ModuleUrl + '/edit', this.formModel.value).subscribe(res => {
      if (res.status == 0) {
        this.baseService.layer.messageSuccess(this.formModel.controls['id'].value > 0 ? "修改成功" : "添加成功");
        this.handleCancel();
        this.editNotice.emit();
        this.isOkLoading = false;
      } else {
        this.baseService.layer.messageError(res.msg);
        this.isOkLoading = false;
      }
    });
  }
}
